#ifndef UPDATER_H
#define UPDATER_H

#include "upacker.h"

#include <PackerConst.h>
#include <QObject>
#include <QTimer>
class Updater : public QObject {
  Q_OBJECT
public:
  explicit Updater(QObject *parent = nullptr);

  QByteArray firmware() const;
  void setFirmware(QByteArray &firmware);

  int32_t packSize() const;
  void setPackSize(const int32_t &packSize);
  void sendUpdateHeader();
  void startDownload();
  void sendFirstPack();
  void sendPack(uint16_t index);
  void setProgress(float value);

  bool getIndexStatus(int i);
  int getIndexBlock(int i);
  bool setIndexStatus(int i, int v);

public slots:
  void packHandler(QByteArray data);
  void unpackData(QByteArray data);
  void updateCaption();

private:
  void stopTimer();

private slots:

signals:
  void sendData(QByteArray data);
  void downloadRunning();
  void downloadProgress(int index, int progress);
  void downloadComplete();

  void downReplyTimeout();
  void ctlReplyTimeout();
  void packReplyTimeout();

  void progressChanged(float);
  void addDownLog(QString msg);

private:
  Upacker *upacker;
  update_header_inst update_header;
  update_worker_inst update_worker;
  QByteArray m_firmware;
  int32_t m_packSize = 1024;
  int32_t timeout = 1000;
  float progress;
  QTimer *timer;
  int m_retry;
};

#endif // UPDATER_H
